package com.android.multiplesmart.adapter;

import android.content.Context;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;

import com.android.multiplesmart.bean.LocaLanguageBean;

import java.util.LinkedList;
import java.util.List;

public class SpinngerAdapter  implements AdapterView.OnItemSelectedListener {
    private  static SpinngerAdapter spinngerAdapter;
    private  ArrayAdapter adapter;
    private List<LocaLanguageBean> list;
    private int layout;
    private Context context;
    private  OnCallAdapter onCallAdapter;
    private  Spinner spinner;
    private   List<String>  name=new LinkedList<>();
    public static   SpinngerAdapter getSpinngerAdapter(){
         if (spinngerAdapter==null){
              synchronized (SpinngerAdapter.class){
                   if (spinngerAdapter==null){
                       spinngerAdapter=new SpinngerAdapter();
                   }
              }
         }
       return     spinngerAdapter;
    }

    public  SpinngerAdapter context(Context context){
        this.context=context;
        return  this;
    }
    public  SpinngerAdapter listData(List<LocaLanguageBean> list){
         this.list=list;
         return  this;
    }
    public  SpinngerAdapter dropdown_item(int layout){
        this.layout=layout;
        return  this;
    }
    public  SpinngerAdapter onCallAdapter(OnCallAdapter onCallAdapter){
        this.onCallAdapter=onCallAdapter;
        return  this;
    }


    public SpinngerAdapter spinner(Spinner spinner){
        this.spinner=spinner;
        return  this;
    }


    public   void getArrayAdapter(){
        // 创建ArrayAdapter，并指定布局文件或者TextView作为下拉菜单的样式
        if(layout==0){
            layout=android.R.layout.simple_spinner_dropdown_item;
        }
        if(name.size()>0){
            name.clear();
        }
        for (int i=0;i<list.size();i++){
            name.add(list.get(i).getLanguageEnName());
        }
        adapter = new ArrayAdapter<String>(context,layout, name);
        // 设置下拉菜单的样式
        adapter.setDropDownViewResource(layout);
        // 将ArrayAdapter返回到使用的activity
        spinner.setAdapter(adapter);
        spinner.setOnItemSelectedListener(this);
    }



    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
        onCallAdapter.adapter(parent,view,position,id);
    }

    @Override
    public void onNothingSelected(AdapterView<?> parent) {

    }


    public  interface  OnCallAdapter{
        void  adapter(AdapterView<?> parent, View view, int position, long id);
    }

}
